package com.gusha.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import java.math.BigInteger;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.gusha.dao.SysLogErrorDao;
import com.gusha.model.SysLogError;


/**
 * @Created：2018-10-26 15:18:16
 * @Author LightSound的代码生成器
 * @Description:SysLogErrorService服务类
 */

@Service
public class SysLogErrorService {

    @Autowired
    private SysLogErrorDao sysLogErrorDao;

    public Page<SysLogError> findAll(Pageable pageable, SysLogError sysLogError) {
        @SuppressWarnings("serial")
        Page<SysLogError> result = sysLogErrorDao.findAll(new Specification<SysLogError>() {
            @Override
            public Predicate toPredicate(Root<SysLogError> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                if (sysLogError.getId() != null) {
                    list.add(cb.equal(root.get("id").as(BigInteger.class),sysLogError.getId()));
                }
                if (StringUtils.isNotBlank(sysLogError.getPath())) {
                    list.add(cb.equal(root.get("path").as(String.class),sysLogError.getPath()));
                }
                if (StringUtils.isNotBlank(sysLogError.getParams())) {
                    list.add(cb.equal(root.get("params").as(String.class),sysLogError.getParams()));
                }
                if (StringUtils.isNotBlank(sysLogError.getExceptionInfo())) {
                    list.add(cb.equal(root.get("exceptionInfo").as(String.class),sysLogError.getExceptionInfo()));
                }
                if (sysLogError.getStartTime() != null) {
                    list.add(cb.greaterThanOrEqualTo(root.get("createTime").as(Date.class),sysLogError.getStartTime()));
                }
                if (sysLogError.getEndTime() != null) {
                	list.add(cb.lessThanOrEqualTo(root.get("createTime").as(Date.class),sysLogError.getEndTime()));
                }
                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
        },pageable);
         return result;
    }

}
